home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_marshal.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  8KB  |  237 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. from test import test_support
  5. import marshal
  6. import sys
  7. import unittest
  8. import os
  9.  
  10. class IntTestCase(unittest.TestCase):
  11.     
  12.     def test_ints(self):
  13.         n = sys.maxint
  14.         while n:
  15.             for expected in (-n, n):
  16.                 s = marshal.dumps(expected)
  17.                 got = marshal.loads(s)
  18.                 self.assertEqual(expected, got)
  19.                 marshal.dump(expected, file(test_support.TESTFN, 'wb'))
  20.                 got = marshal.load(file(test_support.TESTFN, 'rb'))
  21.                 self.assertEqual(expected, got)
  22.             
  23.             n = n >> 1
  24.         os.unlink(test_support.TESTFN)
  25.  
  26.     
  27.     def test_int64(self):
  28.         
  29.         def to_little_endian_string(value, nbytes):
  30.             bytes = []
  31.             for i in range(nbytes):
  32.                 bytes.append(chr(value & 255))
  33.                 value >>= 8
  34.             
  35.             return ''.join(bytes)
  36.  
  37.         maxint64 = (0x1L << 63) - 1
  38.         minint64 = -maxint64 - 1
  39.         for base in (maxint64, minint64, -maxint64, -(minint64 >> 1)):
  40.             while base:
  41.                 s = 'I' + to_little_endian_string(base, 8)
  42.                 got = marshal.loads(s)
  43.                 self.assertEqual(base, got)
  44.                 if base == -1:
  45.                     base = 0
  46.                     continue
  47.                 base >>= 1
  48.         
  49.  
  50.     
  51.     def test_bool(self):
  52.         for b in (True, False):
  53.             new = marshal.loads(marshal.dumps(b))
  54.             self.assertEqual(b, new)
  55.             self.assertEqual(type(b), type(new))
  56.             marshal.dump(b, file(test_support.TESTFN, 'wb'))
  57.             new = marshal.load(file(test_support.TESTFN, 'rb'))
  58.             self.assertEqual(b, new)
  59.             self.assertEqual(type(b), type(new))
  60.         
  61.  
  62.  
  63.  
  64. class FloatTestCase(unittest.TestCase):
  65.     
  66.     def test_floats(self):
  67.         small = 1e-25
  68.         n = sys.maxint * 3.7e+250
  69.         while n > small:
  70.             for expected in (-n, n):
  71.                 f = float(expected)
  72.                 s = marshal.dumps(f)
  73.                 got = marshal.loads(s)
  74.                 self.assertEqual(f, got)
  75.                 marshal.dump(f, file(test_support.TESTFN, 'wb'))
  76.                 got = marshal.load(file(test_support.TESTFN, 'rb'))
  77.                 self.assertEqual(f, got)
  78.             
  79.             n /= 123.4567
  80.         f = 0.0
  81.         s = marshal.dumps(f)
  82.         got = marshal.loads(s)
  83.         self.assertEqual(f, got)
  84.         n = sys.maxint * 3.6999999999999998e-250
  85.         while n < small:
  86.             for expected in (-n, n):
  87.                 f = float(expected)
  88.                 s = marshal.dumps(f)
  89.                 got = marshal.loads(s)
  90.                 self.assertEqual(f, got)
  91.                 marshal.dump(f, file(test_support.TESTFN, 'wb'))
  92.                 got = marshal.load(file(test_support.TESTFN, 'rb'))
  93.                 self.assertEqual(f, got)
  94.             
  95.             n *= 123.4567
  96.         os.unlink(test_support.TESTFN)
  97.  
  98.  
  99.  
  100. class StringTestCase(unittest.TestCase):
  101.     
  102.     def test_unicode(self):
  103.         for s in [
  104.             u'',
  105.             u'Andr\xc3\xa8 Previn',
  106.             u'abc',
  107.             u' ' * 10000]:
  108.             new = marshal.loads(marshal.dumps(s))
  109.             self.assertEqual(s, new)
  110.             self.assertEqual(type(s), type(new))
  111.             marshal.dump(s, file(test_support.TESTFN, 'wb'))
  112.             marshal.load(file(test_support.TESTFN, 'rb'))
  113.             self.assertEqual(s, new)
  114.             self.assertEqual(type(s), type(new))
  115.         
  116.         os.unlink(test_support.TESTFN)
  117.  
  118.     
  119.     def test_string(self):
  120.         for s in [
  121.             '',
  122.             'Andr\xe8 Previn',
  123.             'abc',
  124.             ' ' * 10000]:
  125.             new = marshal.loads(marshal.dumps(s))
  126.             self.assertEqual(s, new)
  127.             self.assertEqual(type(s), type(new))
  128.             marshal.dump(s, file(test_support.TESTFN, 'wb'))
  129.             marshal.load(file(test_support.TESTFN, 'rb'))
  130.             self.assertEqual(s, new)
  131.             self.assertEqual(type(s), type(new))
  132.         
  133.         os.unlink(test_support.TESTFN)
  134.  
  135.     
  136.     def test_buffer(self):
  137.         for s in [
  138.             '',
  139.             'Andr\xe8 Previn',
  140.             'abc',
  141.             ' ' * 10000]:
  142.             b = buffer(s)
  143.             new = marshal.loads(marshal.dumps(b))
  144.             self.assertEqual(s, new)
  145.             marshal.dump(b, file(test_support.TESTFN, 'wb'))
  146.             marshal.load(file(test_support.TESTFN, 'rb'))
  147.             self.assertEqual(s, new)
  148.         
  149.         os.unlink(test_support.TESTFN)
  150.  
  151.  
  152.  
  153. class ExceptionTestCase(unittest.TestCase):
  154.     
  155.     def test_exceptions(self):
  156.         new = marshal.loads(marshal.dumps(StopIteration))
  157.         self.assertEqual(StopIteration, new)
  158.  
  159.  
  160.  
  161. class CodeTestCase(unittest.TestCase):
  162.     
  163.     def test_code(self):
  164.         co = ExceptionTestCase.test_exceptions.func_code
  165.         new = marshal.loads(marshal.dumps(co))
  166.         self.assertEqual(co, new)
  167.  
  168.  
  169.  
  170. class ContainerTestCase(unittest.TestCase):
  171.     d = {
  172.         'astring': 'foo@bar.baz.spam',
  173.         'afloat': 7283.4300000000003,
  174.         'anint': 2 ** 20,
  175.         'ashortlong': 0x2L,
  176.         'alist': [
  177.             '.zyx.41'],
  178.         'atuple': ('.zyx.41',) * 10,
  179.         'aboolean': False,
  180.         'aunicode': u'Andr\xc3\xa8 Previn' }
  181.     
  182.     def test_dict(self):
  183.         new = marshal.loads(marshal.dumps(self.d))
  184.         self.assertEqual(self.d, new)
  185.         marshal.dump(self.d, file(test_support.TESTFN, 'wb'))
  186.         marshal.load(file(test_support.TESTFN, 'rb'))
  187.         self.assertEqual(self.d, new)
  188.         os.unlink(test_support.TESTFN)
  189.  
  190.     
  191.     def test_list(self):
  192.         lst = self.d.items()
  193.         new = marshal.loads(marshal.dumps(lst))
  194.         self.assertEqual(lst, new)
  195.         marshal.dump(lst, file(test_support.TESTFN, 'wb'))
  196.         marshal.load(file(test_support.TESTFN, 'rb'))
  197.         self.assertEqual(lst, new)
  198.         os.unlink(test_support.TESTFN)
  199.  
  200.     
  201.     def test_tuple(self):
  202.         t = tuple(self.d.keys())
  203.         new = marshal.loads(marshal.dumps(t))
  204.         self.assertEqual(t, new)
  205.         marshal.dump(t, file(test_support.TESTFN, 'wb'))
  206.         marshal.load(file(test_support.TESTFN, 'rb'))
  207.         self.assertEqual(t, new)
  208.         os.unlink(test_support.TESTFN)
  209.  
  210.  
  211.  
  212. class BugsTestCase(unittest.TestCase):
  213.     
  214.     def test_bug_5888452(self):
  215.         marshal.dumps([
  216.             128] * 1000)
  217.  
  218.     
  219.     def test_patch_873224(self):
  220.         self.assertRaises(Exception, marshal.loads, '0')
  221.         self.assertRaises(Exception, marshal.loads, 'f')
  222.         self.assertRaises(Exception, marshal.loads, marshal.dumps(0x5L)[:-1])
  223.  
  224.     
  225.     def test_version_argument(self):
  226.         self.assertEquals(marshal.loads(marshal.dumps(5, 0)), 5)
  227.         self.assertEquals(marshal.loads(marshal.dumps(5, 1)), 5)
  228.  
  229.  
  230.  
  231. def test_main():
  232.     test_support.run_unittest(IntTestCase, FloatTestCase, StringTestCase, CodeTestCase, ContainerTestCase, ExceptionTestCase, BugsTestCase)
  233.  
  234. if __name__ == '__main__':
  235.     test_main()
  236.  
  237.